package cn.edu.java.thread.四种实现方式;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableDemo implements Callable<String>{
    @Override
    public String call() {
        System.out.println(Thread.currentThread().getName() + " : 子线程结束！");
        return Thread.currentThread().getName(); // 返回线程名
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CallableDemo callable = new CallableDemo();
        FutureTask<String> futureTask1 = new FutureTask<>(callable);
        FutureTask<String> futureTask2 = new FutureTask<>(callable);

        new Thread(futureTask1).start();
        new Thread(futureTask2).start();

        System.out.println(futureTask1.get());  // 会等待 futureTask1 执行完毕
        System.out.println(futureTask2.get());  // 会等待 futureTask2 执行完毕

        System.out.println(Thread.currentThread().getName() + " : 主线程结束！");


    }
}
